package controllers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.hibernate.mapping.Collection;

import models.User;
import play.Logger;
import play.mvc.With;


@Check(Security.USER_ROLE)
@With(Secure.class)
public class ManagerController extends BaseController {
	
	public static void manage_roles() {
		User user = getCurrentUser();
		Logger.info("User logged -> " + user != null ? user.getProfile().getEmail() : "Unknown!!");
		List<User> users = User.findByDomain(user.getProfile().getDomain()); // TODO use a service todo this
		List<String> roles = Security.getRoles();
		
		render(users, roles);
	}
	
	public static void save_roles() {
		Long userId = request.params.get("user", Long.class);
		String[] roles = request.params.get("roles", String[].class);
		
		User user = User.findById(userId);
		user.setRoles(roles != null ? Arrays.asList(roles) : new ArrayList<String>());
		user.save();
		
		manage_roles(); // TODO render a json message
	}
	
}
